//
// Created by ilong on 2019/12/11.
//

#ifndef THREAD_TOOL_TEST_MEMPORY_POOL_TEST_H
#define THREAD_TOOL_TEST_MEMPORY_POOL_TEST_H

#include <gtest/gtest.h>
#include <object_pool.h>
#include <common.h>
#include <vector>
#include <thread>

class Data{
public:
    Data(){
        LOGI("Data()");
    }
    void mallocBuffer(int size){
        buffer = static_cast<char *>(malloc(size));
    }
    virtual ~Data(){
        if(buffer){
            free(buffer);
            buffer = nullptr;
        }
        LOGI("~Data()");
    }
private:
    char * buffer{nullptr};
};

class ObjTest1: public testing::Test{
protected:
    virtual void SetUp() override{
        obj_pool_ = std::make_shared<ilong::ObjectPool<Data>>(10,"", 1);
    }
    virtual void TearDown() override{

    }
    std::shared_ptr<ilong::ObjectPool<Data>> obj_pool_;
};

TEST_F(ObjTest1, Test1){
    std::vector<std::shared_ptr<Data>> array(5);
    for (auto &item:array) {
        auto data = obj_pool_->getObject();
        array.push_back(data);
    }
    array.clear();
    {
        std::shared_ptr<Data> data1 = obj_pool_->getObject();
        //
        std::this_thread::sleep_for(std::chrono::seconds(2));
    }
    std::shared_ptr<Data> data2 = obj_pool_->getObject();
    std::shared_ptr<Data> data3 = obj_pool_->getObject();

    EXPECT_EQ(obj_pool_->getFreeCount(), 0);
    EXPECT_EQ(obj_pool_->getUsedCount(), 2);

}

#endif //THREAD_TOOL_TEST_MEMPORY_POOL_TEST_H
